//
//  NYModifyPhoneViewController.m
//  Nooryes
//
//  Created by baken on 2020/3/8.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYModifyPhoneViewController.h"
#import "NYEditViewModel.h"
#import "NYLoginViewModel.h"
#import "NYAlertView.h"


@interface NYModifyPhoneViewController ()

@property (nonatomic, strong) NYEditViewModel *editVM;

@property (nonatomic, strong) NYLoginViewModel *loginVM;

@property (nonatomic, strong) NSTimer *countDownTimer;

@property (nonatomic, assign) int secondCount;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UILabel *originalphoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *verifyButton;

@property (weak, nonatomic) IBOutlet UITextField *nophonenew; //第三方登录没有手机号的用户修改手机号填写
@property (weak, nonatomic) IBOutlet UITextField *modifidNewphone; //手机号登录的用户修改手机号填写
@property (weak, nonatomic) IBOutlet UIView *modifiedNewline;
@property (weak, nonatomic) IBOutlet UIView *noPhoneView;

@end

@implementation NYModifyPhoneViewController

-(NYLoginViewModel *)loginVM{
    if (!_loginVM) {
        _loginVM = [NYLoginViewModel new];
    }
    return _loginVM;
}

-(NYEditViewModel *)editVM{
    if (!_editVM) {
        _editVM = [NYEditViewModel new];
    }
    return _editVM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改手机号";
    UIButton *confirm = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
    [confirm setTitle:@"完成" forState:UIControlStateNormal];
    [confirm setTitleColor:UIColorFromRGB(0xff3030) forState:UIControlStateNormal];
    [confirm addTarget:self action:@selector(modifyPhone) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:confirm];
    if (IsStrEmpty(self.phone)) {
        self.modifidNewphone.hidden = YES;
        self.modifiedNewline.hidden = YES;
        self.originalphoneLabel.hidden = YES;
        self.noPhoneView.hidden = NO;
    } else {
        self.originalphoneLabel.text = self.phone;
    }
}

-(void)modifyPhone{
    NSString *phone;
    if (IsStrEmpty(self.phone)) {
        phone = self.nophonenew.text;
    } else {
        phone = self.modifidNewphone.text;
    }
    NSString *regex = @"^[1][3456789][0-9]{9}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:phone];
    if (isValid) {
        self.editVM.phone = phone;
        self.editVM.code = self.codeTextField.text;
        [self.editVM modifyPhoneWithCompletionHandle:^(NSError *error) {
            NYAlertView *alert = [NYAlertView sharedAlertView];
            if (!error) {
                alert.title = @"修改完成";
                [alert showAlert:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                
            } else {
                alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
                [alert showAlert:^{
                }];
            }
        }];
    } else{
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"请输入正确的手机号";
        [alert showAlert:^{
        }];
    }
    
}


- (IBAction)getVerifyCode:(id)sender {
    self.loginVM.phoneNum = IsStrEmpty(self.phone)? self.nophonenew.text:self.phone;
    if (IsStrEmpty(self.loginVM.phoneNum)&& IsStrEmpty(self.phone)) {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"请输入手机号";
        [alert showAlert:^{}];
        return;
    }
    self.secondCount = 60;
    self.verifyButton.userInteractionEnabled = NO;
    self.countDownTimer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    self.loginVM.verifyType = @"4";
    [self.loginVM getVerifyCodeWithCompletionHandle:^(NSError *error) {
        if (!error) {
            self.codeTextField.text = self.loginVM.code;
        } else {
            NYAlertView *alert = [NYAlertView sharedAlertView];
            alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
            [alert showAlert:^{


            }];
        }
    }];
}

- (void)countDown{
    _secondCount--;
    if(_secondCount==0){
        self.verifyButton.userInteractionEnabled = YES;
        self.verifyButton.backgroundColor = [UIColor redColor];
        self.verifyButton.titleLabel.textColor = [UIColor whiteColor];
        self.verifyButton.titleLabel.text = @"获取验证码";
        [self.verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_countDownTimer invalidate];
    } else {
        self.verifyButton.backgroundColor = UIColorFromRGB(0xe3e3e3);
        self.verifyButton.titleLabel.textColor = UIColorFromRGB(0x999999);
        self.verifyButton.titleLabel.text = [NSString stringWithFormat:@"%iS后重新获取",_secondCount];
        [self.verifyButton setTitle:[NSString stringWithFormat:@"%iS后重新获取",_secondCount] forState:UIControlStateNormal];
    }
}
- (void)hideKeyBoard{
    [self.nophonenew resignFirstResponder];
    [self.codeTextField resignFirstResponder];
    [self.modifidNewphone resignFirstResponder];
    
}
@end
